<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :title="title"
    align-center
    draggable
    width="35%"
    style="border-radius: 10px; padding: 5px 10px"
  >
    <el-form :model="form" label-width="120px" :rules="rules">
      <el-form-item label="条目名称">
        <el-select
        filterable
          remote
        style="width: 300px"
          v-model="form.pieceId"
          placeholder="请选择计件条目"
        >
          <el-option
            v-for="(item, index) in PieceRuleList"
            :label="item.name"
            :key="index"
            :value="item.id"
          >
            {{ item.name }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="form.state" />
      </el-form-item>
      <el-form-item label="备注说明" class="ins">
        <el-input v-model="form.ins" type="textarea" style="width: 300px" class="ins"/>
      </el-form-item>
      <el-form-item>
        
        <el-button style="width: 100px" type="primary" @click="submitForm"
          >创建</el-button
        >
        <el-button style="width: 100px" @click="onClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted, reactive, defineProps, defineEmits ,watch,onBeforeMount} from "vue";
import { addAssessor, getRuleList,updateAssessor } from "@/api/piece";
import { ElMessage } from "element-plus";

const form = reactive({
  id:"",
  pieceId: "",
  positionId: "",
  state: true,
  ins: "",
});

const dialogFormVisible = ref(true);
const props = defineProps({
  dialogFormVisible: {
    type: Boolean,
    required: true,
  },
  fourthId: {
    type: Number,
    required: true,
  },
  id: {
    type: Number,
    required: true, //是否规定必须得有
  },
  close: {
    type: Function,
    default: () => {},
  },
  update: {
    type: Function,
    default: () => {},
  },
  selectContent: {
    type: Object,
    required: true,
  },
  dialogType: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
});

const PieceRuleList = ref();
onBeforeMount(() => {
  getRuleList().then((data) => {
    PieceRuleList.value = data.data;
  });
});

watch(props, (newValue, oldValue) => {
  if (newValue.dialogType == "add") {
    form.pieceId = newValue.selectContent.pieceId;
    form.positionId = newValue.selectContent.positionId;
    form.state = newValue.selectContent.state;
    form.ins = newValue.selectContent.ins;
  } 
  else if (newValue.dialogType == "edit") {
    form.id=newValue.selectContent.positionPieceId;
    form.pieceId = newValue.selectContent.pieceId;
    form.positionId = newValue.selectContent.positionId;
    form.state = newValue.selectContent.scoreState== 1 ? true : false;
    form.ins = newValue.selectContent.ins;
  }
});

const onClose = () => {
  props.close();
};

const submitForm = () => {
  form.positionId = props.id.id;
  if (props.dialogType == "add") {
    addAssessor(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "新增成功",
          type: "success",
        });
        props.close();
        props.update();
      } else {
        ElMessage.error("新增失败");
      }
    });
  } 
  else if (props.dialogType == "edit") {
    updateAssessor(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "修改成功",
          type: "success",
        });
        props.close();
        props.update();
      } else {
        ElMessage.error("修改失败");
      }
    });
  }
};
</script>
<style lang="scss" scoped >
::v-deep .el-form-item__label:before {
  content: "* ";
  color: red;
}
.ins {
  ::v-deep .el-form-item__label:before {
    content: " ";
  }
}
</style>